/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package SystemTray;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import GuiTools.GuiTools;
import GuiTools.LabelPanel;
import GuiTools.TribuitWordChooser;
import Mainframe.Mainframe;
import Settings.Language;
import Settings.Settings;
import Storage.Storage;
import Storage.Vocabulary;
import Storage.Word;
import VMAddManyWords.AddManyWords;
import VocabularyManagement.EditWordDialog;

/**
 * @author Christoph Schnack
 *
 */
public class TribuitSystemTrayMenu extends PopupMenu {
	Storage MAINVOC;
	Settings SET;
	Mainframe MAINFRAME;
	Language LANG;
	Menu addWordMenu,addWordsMenu,popUpLearningMenu;
	MenuItem exitItem, restoreItem,pulSettings,pulStart,pulStop;
	PopUpWords popUpWords;
	PopupMenu popup;
	SystemTray tray;
	PuwSettings settingsDialog;
	
	public TribuitSystemTrayMenu(Storage mv, Settings set, Mainframe mf,SystemTray tr){
		super();
		popup=this;
		MAINVOC=mv;
		MAINFRAME=mf;
		SET=set;
		LANG = SET.getLanguage();
		tray=tr;
    	restoreItem = new MenuItem("Tribuit");
    	restoreItem.addActionListener(new DefaultListener());
    	exitItem = new MenuItem(SET.getLanguage().getQuit());
    	exitItem.addActionListener(new QuitListener());
    	addWordMenu = new Menu(LANG.getAddWordTo());
    	addWordsMenu = new Menu(LANG.getAddWordsTo());
    	if(MAINVOC.hasVocs()){
    		Vocabulary[] vocs = MAINVOC.getVocabularies();
    		for(Vocabulary v : vocs){
    			final String vName = v.getName();
    			MenuItem tmpItem1 = new MenuItem(vName);
    			tmpItem1.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					new EditWordDialog(vName,MAINVOC,SET,MAINFRAME);
    				}
    			});
    			addWordMenu.add(tmpItem1);
    			MenuItem tmpItem2 = new MenuItem(vName);
    			tmpItem2.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					new AddManyWords(vName,MAINVOC,SET,MAINFRAME);
    				}
    			});
    			addWordsMenu.add(tmpItem2);
    		}
    	}
    	else{
    		addWordsMenu.setEnabled(false);
    		addWordMenu.setEnabled(false);
    	}
    	pulStart=new MenuItem("start");
    	pulStart.addActionListener(new PulStartListener());
    	pulStop=new MenuItem("stop");
    	pulStop.setEnabled(false);
    	pulStop.addActionListener(new PulStopListener());
    	pulSettings=new MenuItem(LANG.getSettings());
    	pulSettings.addActionListener(new PulSettingsListener());
    	popUpLearningMenu=new Menu("pop up "+LANG.getWords());
    	popUpLearningMenu.add(pulStart);
    	popUpLearningMenu.add(pulStop);
    	popUpLearningMenu.add(pulSettings);
    	popup.add(restoreItem);
    	popup.add("-");
    	popup.add(addWordsMenu);
    	popup.add(addWordMenu);
    	popup.add("-");
    	popup.add(popUpLearningMenu);
    	popup.add("-");
    	popup.add(exitItem);
	}
	
	public void setVocabularyMenus(){
		if(MAINVOC.hasVocs()){
			addWordsMenu.removeAll();
			addWordMenu.removeAll();
			addWordsMenu.setEnabled(true);
    		addWordMenu.setEnabled(true);
    		Vocabulary[] vocs = MAINVOC.getVocabularies();
    		for(Vocabulary v : vocs){
    			final String vName = v.getName();
    			MenuItem tmpItem1 = new MenuItem(vName);
    			tmpItem1.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					new EditWordDialog(vName,MAINVOC,SET,MAINFRAME);
    				}
    			});
    			addWordMenu.add(tmpItem1);
    			MenuItem tmpItem2 = new MenuItem(vName);
    			tmpItem2.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					new AddManyWords(vName,MAINVOC,SET,MAINFRAME);
    				}
    			});
    			addWordsMenu.add(tmpItem2);
    		}
    	}
    	else{
    		addWordsMenu.setEnabled(false);
    		addWordMenu.setEnabled(false);
    	}
	}
	
	class QuitListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			GuiTools.exitTribuit(MAINVOC, MAINFRAME, SET);
		}
	}
	
	class DefaultListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			MAINFRAME.setVisible(true);
			if(MAINFRAME.getState()==JFrame.ICONIFIED) MAINFRAME.setState(JFrame.NORMAL);
		}
	}
	
	class PulStartListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(popUpWords==null){
				if(settingsDialog==null || !settingsDialog.isDisplayable()) settingsDialog = new PuwSettings(MAINVOC,SET,MAINFRAME,true);
				else{
					if(settingsDialog.preTestFrame.getState()==JFrame.ICONIFIED)settingsDialog.preTestFrame.setState(JFrame.NORMAL);
					settingsDialog.showUp();
				}
			}
			else{
				popUpWords.start();
				pulStop.setEnabled(true);
				pulStart.setEnabled(false);
			}
		}
	}
	class PulStopListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			popUpWords.stop();
			pulStop.setEnabled(false);
			pulStart.setEnabled(true);
		}
	}
	
	class PulSettingsListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(settingsDialog==null || !settingsDialog.isDisplayable()) settingsDialog = new PuwSettings(MAINVOC,SET,MAINFRAME,false);
			else{
				if(settingsDialog.preTestFrame.getState()==JFrame.ICONIFIED)settingsDialog.preTestFrame.setState(JFrame.NORMAL);
				settingsDialog.showUp();
			}
		}
	}
	
	class PuwSettings extends TribuitWordChooser{
		JSpinner timeSpinner;
		boolean startPopUp;
		public PuwSettings(Storage mv, Settings set, Mainframe mf,boolean start){
			super(mv,set,mf,"pop up " + set.getLanguage().getWords(),set.getLanguage().getDisplayed());
			this.buildGUI();
			startPopUp=start;
		}
		

		public JPanel optionPanel(){
			JPanel optionsPanel = new JPanel();
			SpinnerModel model = new SpinnerNumberModel(10,1,60,1);
			timeSpinner = new JSpinner(model);
			JPanel spinnerPanel =new JPanel();
			spinnerPanel.add(timeSpinner);
			spinnerPanel.add(new JLabel(LANG.getSec()));
			optionsPanel.add(new LabelPanel(LANG.getNewWordAfter(),spinnerPanel));
			return optionsPanel;
		}
		public void okAction(String vocName,Vocabulary voc){
			popUpWords = new PopUpWords(this.getTestLanguage2(),testvocabulary.toArray(new Word[]{}),((Integer)timeSpinner.getValue())*1000,tray);
			if(startPopUp)pulStart.getActionListeners()[0].actionPerformed(new ActionEvent(new MenuItem("pseudo"),12345,"pseudo"));
		}
		
		public boolean isDisplayable(){return preTestFrame.isDisplayable();}
		public void showUp(){preTestFrame.showUp(300,100);}

	}
	
	public void updateLanguage(){
		LANG = SET.getLanguage();
		exitItem.setLabel(LANG.getQuit());
		addWordsMenu.setLabel(LANG.getAddWordsTo());
		addWordMenu.setLabel(LANG.getAddWordTo());
		pulSettings.setLabel(LANG.getSettings());
		popUpLearningMenu.setLabel("pop up "+LANG.getWords());
	}
}
